package com.etc.delivery.controller.user;

import com.etc.delivery.entity.User;
import com.etc.delivery.service.user.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;


@SessionAttributes(value = {"msg", "phone"})
@RequestMapping("/user")
@Controller("userController01")
public class UserController {

    @Resource(name = "userServiceImpl01")
    private IUserService iUserService;

    @RequestMapping("/register")
    public ModelAndView register(User user) {
        ModelAndView mv = new ModelAndView();
        List<User> list = iUserService.queryUserPhone1(user.getPhone());
        if (!list.isEmpty()) {
            mv.addObject("msg1", "号码不可用");
            mv.setViewName("redirect:/user/userRegister.jsp");
            return mv;
        }
        boolean flag = iUserService.register(user);
        if (flag) {
            mv.addObject("msg1", "注册成功");
            mv.setViewName("redirect:userLogin.jsp");
        } else {
            mv.addObject("msg1", "注册失败");
            mv.setViewName("redirect:/user/userRegister.jsp");
        }

        return mv;

    }

    @RequestMapping("/userLogin")
    public ModelAndView userLogin(String phone, String password, HttpSession session) {
        ModelAndView mv = new ModelAndView();
        User user = iUserService.userLogin(phone, password);
        if (user != null) {
            mv.addObject("msg", "登录成功");
            mv.addObject("phone", phone);
            session.setAttribute("phone", phone);
            mv.setViewName("redirect:index.jsp");
        } else {
            mv.addObject("msg", "登录失败,您的账号已注销");
            mv.setViewName("redirect:userLogin.jsp");
        }
        return mv;
    }

    @RequestMapping("/queryUserPhone")
    public ModelAndView queryUserPhone(String phone) {
        ModelAndView mv = new ModelAndView();
        User user = iUserService.queryUserPhone(phone);
        mv.addObject("user", user);
        mv.setViewName("forward:person.jsp");
        return mv;
    }

    @RequestMapping("/queryUserPhone1")
    public ModelAndView queryUserPhone1(String phone) {
        ModelAndView mv = new ModelAndView();
        User user = iUserService.queryUserPhone(phone);
        mv.addObject("user", user);
        mv.setViewName("forward:/user/userMail.jsp");
        return mv;
    }

    @RequestMapping("/queryUserPhone2")
    public ModelAndView queryUserPhone2(String phone) {
        ModelAndView mv = new ModelAndView();
        User user = iUserService.queryUserPhone(phone);
        mv.addObject("user", user);
        mv.setViewName("forward:/user/userMails.jsp");
        return mv;
    }

    @RequestMapping("/updateUser")
    public ModelAndView updateUser(User user) {
        ModelAndView mv = new ModelAndView();
        boolean flag = iUserService.updateUser(user);
        if (flag) {
            mv.addObject("msg", "修改成功");
            mv.setViewName("forward:index.jsp");
        } else {
            mv.addObject("msg", "修改失败");
            mv.setViewName("redirect:/person.jsp");
        }
        return mv;
    }

    @RequestMapping("/updateBalance2")
    public ModelAndView updateBalance2(double balance, String phone) {
        ModelAndView mv = new ModelAndView();
        boolean flag = iUserService.updateBalance2(balance, phone);
        if (flag) {
            mv.addObject("msg", "充值成功");
            mv.setViewName("forward:index.jsp");
        } else {
            mv.addObject("msg", "充值失败");
            mv.setViewName("forward:index.jsp");
        }
        return mv;
    }


}
